﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ClienteBLL
/// </summary>
public class ClienteBLL
{
    public static List<Cliente> GetClientes(){
        DSClienteTableAdapters.ClientesDSTableAdapter tableAdapter = new DSClienteTableAdapters.ClientesDSTableAdapter();
        DSCliente.ClientesDSDataTable dataTable = tableAdapter.GetClientes();
        List<Cliente> listaClientes = new List<Cliente>();
        if (dataTable != null)
        {
            foreach (DSCliente.ClientesDSRow row in dataTable)
            {
                Cliente cliente = RowToDTO(row);
                listaClientes.Add(cliente);
            }
        }
        return listaClientes;
    }

    public static List<Cliente> GetAvailableClientes()
    {
        DSClienteTableAdapters.ClientesDSTableAdapter tableAdapter = new DSClienteTableAdapters.ClientesDSTableAdapter();
        DSCliente.ClientesDSDataTable dataTable = tableAdapter.GetAvailableClients();
        List<Cliente> listaClientes = new List<Cliente>();
        if (dataTable != null)
        {
            foreach (DSCliente.ClientesDSRow row in dataTable)
            {
                Cliente cliente = RowToDTO(row);
                listaClientes.Add(cliente);
            }
        }
        return listaClientes;
    }

    public static Cliente GetClienteByID(int cliente_id)
    {
        DSClienteTableAdapters.ClientesDSTableAdapter tableAdapter = new DSClienteTableAdapters.ClientesDSTableAdapter();
        DSCliente.ClientesDSDataTable dataTable = tableAdapter.GetClienteByID(cliente_id);
        if (dataTable != null)
        {
            return RowToDTO(dataTable[0]);
        }
        return null;
    }

    public static void InsertCliente(Cliente cliente)
    {
        DSClienteTableAdapters.ClientesDSTableAdapter tableAdapter = new DSClienteTableAdapters.ClientesDSTableAdapter();
        if(cliente != null && cliente.sCliente_nm.Trim() != ""){
            tableAdapter.InsertCliente(cliente.sCliente_nm);
        }
    }

    public static Cliente RowToDTO(DSCliente.ClientesDSRow row)
    {
        return (row != null)? new Cliente(row.iCliente_id, row.sCliente_nm): null;
    }
}